#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging

class Event:
    def __init__(self):
        self.handlers = set()

    def handle(self, handler):
        self.handlers.add(handler)
        return self

    def unhandle(self, handler):
        try:
            self.handlers.remove(handler)
        except:
            raise ValueError("Handler is not handling this event, so cannot unhandle it.")
        return self

    def fire(self, *args, **kargs):
        for handler in self.handlers:
            handler(*args, **kargs)

    def getHandlerCount(self):
        return len(self.handlers)

    __iadd__ = handle
    __isub__ = unhandle
    __call__ = fire
    __len__  = getHandlerCount


class Message:
    pass

class Response(Message):
    def __init__(self, message):
        pass

class Request(Message):
    def __init__(self):
        self.PreParseEvent = Event()
        self.PostParseEvent = Event()
    def parse(self, message):
        if 
        self.PreParseEvent( message )
        self.__parse__(message)
        self.PostParseEvent(  )
    def __parse__(self, object):
        pass

class Context:
    def __init__(self):
        self.handlers = set()
    def AddHandle(self, Request):
        self.handlers.add(Request)
        return self
    def Execute(self, *args, **kargs):
        for handler in self.handlers:
            handler.parse(*args, **kargs)
       

class Connect(Request):
    def __parse__(self, message):
        if message != 'connect':
            return
        logging.warning('Connect message parse')
        return 'Connect message was handled'
           
class GetData(Request):
    def __parse__(self, message):
        if message != 'connect':
            return
        logging.warning('Connect message parse')
        return 'Connect message was handled'
    


import uuid
#        self.send('Request: "%s" from id:%s' % (message, self.id) )

class Main:
    def __init__(self):
        self.RequestContext = Context()
        _connect = Connect()
        _connect.PostParseEvent += self.on_response_my
        self.RequestContext.AddHandle(_connect)
    
    def on_response(self, message):
        pass
    
    def on_request(self, message):
        self.on_response("RESPONSE ON MSG: " + message)
        self.RequestContext.Execute(message)
        
    def on_response_my(self, message):
        self.on_response("RESPONSE ON MSG: " + message)